<?php

class dealerModel extends Model{
    public function cancellation($page,$pageSize,$where,$value){
        $str="";
        if($where){
            $str=$this->transformation($where,$value);
        }
        $sql = "select wc.id id,d.name,d.mobile,d.responsible,wc.ctime ctime,wc.status status from wf_cancellation wc left join wf_dealer d on wc.dealer_id=d.id $str order by wc.id desc limit $page,$pageSize";
        return $this->query($sql)->fetchAll();
    }


    public function cancellationtotal($where,$value){
        $str="";
        if($where){
            $str=$this->transformation($where,$value);
        }
        $sql = "select count(wc.id) as total from wf_cancellation wc left join wf_dealer d on wc.dealer_id=d.id ".$str;
        $count= $this->query($sql)->fetchAll();
        return $count[0]['total'];
    }
    public function updatecancellation($ids,$type)
    {
        $this->_tableName = "wf_cancellation";
        $this->_primaryKey = "id";
        $info = $this->find($ids);
        if ($type != 3) {
            if ($info['status'] != 0) {
                return 2;
            }
            $data['status'] = 1;
            if ($this->update($data, "id=?", $ids)) {
                if ($type == 1) {
                    $sql = "Update wf_dealer set status=2 where id=" . $info['dealer_id'];
                    $this->query($sql);
                }
                return true;
            }
        } else {
            if ($info['status'] == 2) {
                $where[] = "id=?";
                $value[] = $ids;
                if ($this->delete($where, $value)) {
                    return true;
                }
            }
        }
        return false;
    }

    private function transformation($where,$value){
        $str="";
        foreach ($where as $k=>$v){
            if(empty($str)){
                $str="where ".$v.$value[$k];
            }else{
                $str.=" and ".$v.$value[$k];
            }
        }
        return $str;
    }

    /**
     * 经销商状态列表
     * @return array
     */
    public function getStatusList() {
        return [
            1 => '正常',
            2 => '已注销',
            3 => '资金冻结'
        ];
    }

    /**
     * 是否为默认经销商状态
     * @return array
     */
    public function getDefaultDealer() {
        return [
            0 => '不是',
            1 => '是'
        ];
    }

    /**
     * 商家录入校验
     * @param $param
     * @return array
     */
    function dealerRule($param, $dealer_id =0)
    {
        if (empty($param['name'])) {
            return ['code' => -1, 'msg' => '请填写名称'];
        }
        if (mb_strlen($param['name']) > 30) {
            return ['code' => -1, 'msg' => '名称不能超过30个字符'];
        }
        if(!preg_match("/^1[34578]{1}\d{9}$/",$param['mobile'])){
            return ['code' => -1, 'msg' => '请填写正确电话'];
        }
        if (empty($param['responsible'])) {
            return ['code' => -1, 'msg' => '请填写负责人'];
        }
        if (mb_strlen($param['responsible']) > 30) {
            return ['code' => -1, 'msg' => '负责人不能超过30个字符'];
        }
        if (!$param['local_province'] || !$param['local_city'] || !$param['local_area']) {
            return ['code' => -1, 'msg' => '所在城市填写不完整'];
        }
        if (empty($param['local_address'])) {
            return ['code' => -1, 'msg' => '请填写详细地址'];
        }
        if (mb_strlen($param['local_address']) > 100) {
            return ['code' => -1, 'msg' => '详细地址不能超过100个字符'];
        }
        // 组装成各一组省市区
        $new_term = [];
        foreach ($param['provinces'] as $key => $value) {
            $province = $value;
            $city = $param['citys'][$key];
            $area = $param['areas'][$key];
            if (!$province || !$city || !$area) {
                return [
                    'code' => -1,
                    'msg' => "经营地区第" . ($key + 1) . "条填写不完整"
                ];
                break;
            }
            //判断是否在经营范围内
            $business_area = self::model('dealer_business')->getOne('area=?', $area);
            if (!$business_area) {
                return [
                    'code' => -1,
                    'msg' => "经营地区第" . ($key + 1) . "条不在品牌商的经营范围内"
                ];
                break;
            }
            //判断是否已经有他经销商
            if ($dealer_id) { //编辑
                $is_exist = self::model('dealer_address')->getOne(['area=?', 'dealer_id!=?'], [$area,$dealer_id]);
            } else { //新增
                $is_exist = self::model('dealer_address')->getOne('area=?', $area);
            }
            if ($is_exist) {
                return [
                    'code' => -1,
                    'msg' => "经营地区第" . ($key + 1) . "条已有其他经销商"
                ];
                break;
            }

            $new_term[$key]['province'] = $province;
            $new_term[$key]['city'] = $city;
            $new_term[$key]['area'] = $area;
        }
        // 判断是否有重复
        if (count(array_unique($new_term,SORT_REGULAR)) != count($new_term)) {
            return [
                'code' => -1,
                'msg' => "经营地区有重复，请检查"
            ];
        }
        if (empty($param['brank_name'])) {
            return ['code' => -1, 'msg' => '请填写银行名称'];
        }
        if (empty($param['brank_number'])) {
            return ['code' => -1, 'msg' => '请填写银行卡号'];
        }

        return [
            'code' => 0,
            'msg'  => 'suc',
            'new_dealer_address' => $new_term
        ];
    }

    public function getGoods($dealer_id){
            $sql="select goods_id from wf_dealer_goods where is_upper=1 and dealer_id=".$dealer_id;
            $goods=$this->query($sql)->fetchAll();
            $goods_id=array();
            if(!empty($goods)){
                foreach ($goods as $v){
                    $goods_id[]=$v['goods_id'];
                }
            }else{
                return 0;
            }
           return implode(',',$goods_id);

    }

    public function dealerInfo($uid){
         $info=self::model('user_address')->getOne(['uid=?'],[$uid]);
         if(empty($info)){
             return -1;
            }else{
            $business=self::model('dealer_business')->getOne(['area=?'],[$info['area_code']]);
            $dealer_address=self::model('dealer_address')->getOne(['area=?','status=?'],[$info['area_code'],1]);
            if(empty($business)){
                return -2;
            }else{
                if(!empty($dealer_address)){
                    return $dealer_address['dealer_id'];
                }else{
                    return 0;
                }
         }
        }
    }

    public function isDefault(){
        $dealer=$this->getOne('is_default=?',1);
        return $dealer['id'];
    }

}

